﻿using System.Web;
using System;
using System.Collections.Generic;
using System.Web.Routing;
namespace Lbb.Web.Mvc
{
    /// <summary>
    /// 数据列表
    /// </summary>
    /// <typeparam name="T">数据项类型</typeparam>
    /// <author>Chiron</author>
    public class ExtDataList<T> : DataList<T>
    {
        #region Fields
        /// <summary>
        /// 当前请求
        /// </summary>
        protected readonly HttpRequestBase Request;
        protected readonly RouteData RouteData;
        protected const string se = ",";
        protected static readonly char[] sec = se.ToCharArray();


        private readonly bool _FindInRequestParams;
        #endregion Fields

        #region Props
        /// <summary>
        /// 关键字
        /// </summary>
        public override string Keyword
        {
            get
            {
                return RouteValues.keyword as string ?? string.Empty;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    RouteValueData.Remove("keyword");
                }
                else
                {
                    RouteValues.keyword = value;
                }
            }
        }

        /// <summary>
        /// 排序
        /// </summary>
        public override string OrderBy
        {
            get
            {
                return RouteValues.orderBy as string ?? string.Empty;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    RouteValueData.Remove("orderBy");
                }
                else
                {
                    RouteValues.orderBy = value;
                }
            }
        }
        #endregion Props

        #region Ctors
        /// <summary>
        /// 构建数据列表
        /// </summary>
        public ExtDataList(RouteData routeData = null, HttpRequestBase request = null, bool initArgs = true, bool findInRequestParams = true)
        {
            _FindInRequestParams = findInRequestParams;

            Request = request ?? (HttpContext.Current != null ? new HttpRequestWrapper(HttpContext.Current.Request) : null);
            RouteData = routeData ?? (Request != null ? Request.RequestContext.RouteData : null);

            CurrentPageKey = "pageNumber";
            PageSizeKey = "pageSize";
            if (initArgs && Request != null)
            {
                int tInt;

                if (int.TryParse(GetReqString(CurrentPageKey), out tInt)) CurrentPage = tInt;
                if (int.TryParse(GetReqString(PageSizeKey), out tInt)) PageSize = tInt;
                Keyword = GetReqString("keyword");
                OrderBy = GetReqString("orderBy");
            }
        }
        #endregion Ctors

        #region Methods

        #region Helpers
        /// <summary>
        /// 取得请求参数
        /// </summary>
        /// <param name="key">参数键</param>
        protected string GetReqString(string key)
        {
            if (string.IsNullOrEmpty(key)) throw new ArgumentNullException("key");
            if (Request == null) return string.Empty;

            object value = RouteData.Values[key];
            if (value != null) return value.ToString();

            if (_FindInRequestParams && (value = Request[key]) != null) return value as string;

            return (RouteData.DataTokens[key] ?? string.Empty).ToString();
        }
        /// <summary>
        /// 取得请求参数
        /// </summary>
        /// <param name="key">参数键</param>
        /// <returns></returns>
        protected bool? GetReqBool(string key)
        {
            var value = GetReqString(key);
            bool tVal;
            return !string.IsNullOrEmpty(value) && bool.TryParse(value, out tVal) ? tVal : null as bool?;
        }
        /// <summary>
        /// 取得请求参数
        /// </summary>
        /// <param name="key">参数键</param>
        /// <returns></returns>
        protected int? GetReqInt(string key)
        {
            var value = GetReqString(key);
            int tVal;
            return !string.IsNullOrEmpty(value) && int.TryParse(value, out tVal) ? tVal : null as int?;
        }
        /// <summary>
        /// 取得请求参数
        /// </summary>
        /// <param name="key">参数键</param>
        /// <returns></returns>
        protected decimal? GetReqDecimal(string key)
        {
            var value = GetReqString(key);
            decimal tVal;
            return !string.IsNullOrEmpty(value) && decimal.TryParse(value, out tVal) ? tVal : null as decimal?;
        }

        /// <summary>
        /// 取得Guid请求参数
        /// </summary>
        /// <param name="key">参数键</param>
        /// <returns></returns>
        protected Guid? GetReqGuid(string key)
        {
            var value = GetReqString(key);
            Guid tVal;
            return !string.IsNullOrEmpty(value) && Guid.TryParse(value, out tVal) ? tVal : null as Guid?;
        }
        #endregion Helpers

        #endregion Methods


    }
}